﻿ 
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq; 
using NTTDATA.BEENChina.ResultCertification.Result;

namespace NTTDATA.BEENChina.ResultCertification.Util
{
    /// <summary>
    /// 变化录入结果中的日期项目
    /// 变化内容 年号 4-平成
    ///         上下午 1,2 午前 午后
    ///     
    ///         其他内容 mm dd hh mm ss 一位数变成2位数 01
    /// </summary>
    public class DateStrHelper 
    { 

        public static  string ChangeDateStr(string  strList,string dateFormat)
        {

            List<string> entryArray = strList.Split(new char[] { '/' }).ToList();

            IEnumerable<string> changeArray = ChangeDateStr(entryArray, dateFormat);

            return string.Join("/", changeArray);
        }


        private static IEnumerable<string> ChangeDateStr(List<string> strList, string dateFormat)
        {
            if (strList == null)
            {
                return null;
            }
            List<string> dateFormatParts = dateFormat.Split(new char[] { '/' }).ToList();

            int yyIndex = dateFormatParts.IndexOf("yy");
            int ggIndex = dateFormatParts.IndexOf("gg");
            int ttIndex = dateFormatParts.IndexOf("tt");
            int MMIndex = dateFormatParts.IndexOf("MM");
            int ddIndex = dateFormatParts.IndexOf("dd");

            int hhIndex = dateFormatParts.IndexOf("hh");
            int HHIndex = dateFormatParts.IndexOf("HH");
            if (HHIndex == -1)
                HHIndex = dateFormatParts.IndexOf("hh");

            int mmIndex = dateFormatParts.IndexOf("mm");
            int ssIndex = dateFormatParts.IndexOf("ss");
            
            

            if (ggIndex > -1)
            {
                try
                {

                    if (yyIndex > -1)
                    {
                    
                        //2013 - 11- 12 modify
                        // 9/1969/11/24
                        if (strList[yyIndex].Length.Equals(4))
                        {
                            var gannen = GetGannnennB(strList[yyIndex]);
                            strList[yyIndex] = gannen["yy"];

                            if (strList[ggIndex].Equals("9"))
                            {
                                strList[ggIndex] = gannen["gg"];
                            }
                        }
                    }
                    strList[ggIndex] = Changegg(strList[ggIndex]);

                }
                catch (Exception ex)
                {

                    Log.Console.Error("SplitEntryResult.GetDateTime", ex);
                }

            }
            if (ttIndex > -1)
            {
                strList[ttIndex] = Changett(strList[ttIndex]);
            }
            if (MMIndex > -1)
            {
                strList[MMIndex] = CompleMonthOrDay(strList[MMIndex]);
            }
            if (ddIndex > -1)
            {
                strList[ddIndex] = CompleMonthOrDay(strList[ddIndex]);
            }


            if (hhIndex > -1)
            {
                strList[hhIndex] = CompleHour(strList[hhIndex]);
            }
            if (HHIndex > -1)
            {
                strList[HHIndex] = CompleHour(strList[HHIndex]);

                //如果出现上下午
                //修正 pm 18  -> pm 06
                if (ttIndex > -1 && !string.IsNullOrEmpty(strList[HHIndex]) && !strList[HHIndex].Contains("?")  )
                {
                    if (strList[ttIndex].Equals("午後")　||  strList[ttIndex].Equals("午前"))
                    {
                        int hour = 0;

                        int.TryParse(strList[HHIndex], out hour );
                        if (  hour > 12)
                        {
                            hour -= 12;

                            strList[HHIndex] =CompleHour( hour.ToString(CultureInfo.InvariantCulture));
                        }

                    }
                }
            }
            if (mmIndex > -1)
            {
                strList[mmIndex] = CompleHour(strList[mmIndex]);
            }
            if (ssIndex > -1)
            {
                strList[ssIndex] = CompleHour(strList[ssIndex]);
            }
            return strList;
        }

        private static string Changegg(string str)
        {
             

            if (string.IsNullOrEmpty(str))
            {
                return str;
            }

            return  GetGannnenn(str).ToString(); 
            
        }

        private static string Changett(string str)
        {
            string tt = string.Empty;
            switch (str)
            {
                case "1":
                    tt = "午前";
                    break;
                case "2":
                    tt = "午後";
                    break;
            }
            return tt;
        }       

        public static EnumGannnenn GetGannnenn(string result)
        {
            EnumGannnenn gannnenn = EnumGannnenn.大正;
            switch (result)
            {
                case "1":
                    gannnenn = EnumGannnenn.明治;
                    break;
                case "2":
                    gannnenn = EnumGannnenn.大正; 
                    break;
                case "3":
                    gannnenn = EnumGannnenn.昭和;                    
                    break;
                case "4":
                case "9":
                    gannnenn = EnumGannnenn.平成;
                    break;
                //default :
                //    gannnenn = EnumGannnenn.平成;
                //    break;
            }

            return gannnenn;
        }


        private static string CompleMonthOrDay(string str)
        {
            int number = 0;

            int.TryParse(str, out number);

            if (number >  0)
            {
                return ((100 + number).ToString(CultureInfo.InvariantCulture)).Substring(1);
            }
            else
            {
                return str;
            }
        }

        private static string CompleHour(string str)
        { 
            int number = 0;

            int.TryParse(str, out number);

            if (number >= 0)
            {
                return ((100 + number).ToString(CultureInfo.InvariantCulture)).Substring(1);
            }
            else
            {
                return str;
            }
        }

        public static Dictionary<string,string> GetGannnennB(string year)
        {
            CultureInfo ci = new CultureInfo("ja-JP");
            Calendar cal = new JapaneseCalendar();
            ci.DateTimeFormat.Calendar = cal;
            DateTime dt = DateTime.Parse(string.Format("{0}/01/01", year));

            string yy =  dt.ToString("yy", ci);
            string gg =  cal.GetEra(dt).ToString(CultureInfo.InvariantCulture); ;

            var values = new Dictionary<string, string> {{"gg", gg}, {"yy", yy}};

            return values;

        }
    }
}
